package com.atguigu.flink0624.chapter11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Json;
import org.apache.flink.table.descriptors.Kafka;
import org.apache.flink.table.descriptors.Schema;

// 静态导入

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/19 10:26
 */
public class Flink03_Table_Connector_Kafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        tenv
            .connect(
                new Kafka()
                    .version("universal")
                    .property("bootstrap.servers", "hadoop162:9092")
                    .property("group.id", "Flink03_Table_Connector_Kafka")
                    .topic("s1")
                    .startFromLatest()
            )
            //            .withFormat(new Csv())
            .withFormat(new Json())
            .withSchema(
                new Schema()
                    .field("id", DataTypes.STRING())
                    .field("ts", DataTypes.BIGINT())
                    .field("vc", DataTypes.INT())
            )
            .createTemporaryTable("sensor");
        
        Table sensor = tenv.from("sensor").select("id, vc");
        
        //
        tenv
            .connect(
                new Kafka()
                    .version("universal")
                    .property("bootstrap.servers", "hadoop162:9092")
                    .topic("s2")
                    .sinkPartitionerRoundRobin()
            
            )
            //            .withFormat(new Csv().lineDelimiter(""))
            .withFormat(new Json())
            .withSchema(
                new Schema()
                    .field("id", DataTypes.STRING())
                    .field("vc", DataTypes.INT())
            )
            .createTemporaryTable("sensor_1");
        
        sensor.executeInsert("sensor_1");
        
    }
}
